// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const TcbRouter = require('tcb-router')
const db = cloud.database()
const _ = db.command;

// 云函数入口函数
exports.main = async (event, context) => {
	const app = new TcbRouter({ event })
	let { OPENID, APPID } = cloud.getWXContext()
	const { addPet, updateId, updateData, queryData, addPetIdentify, identifyId, petId } = event

	app.use(async (ctx, next) => { //适用于所有的路由
		ctx.data = {} //声明data为一个对象
		await next();
	})

	app.router('create', async (ctx, next) => {
		try {
			const addData = await db.collection('pet').add({
				data: {
					...addPet,
					open_id: OPENID,
					create_time: new Date()
				}
			})
			ctx.body = { code: 200, msg: "成功", data: addData }
		} catch (error) {
			ctx.body = { code: 301, msg: "失败", data: {} }
		}
	})

	app.router('update', async (ctx, next) => {
		try {
			const getData = await db.collection('pet').doc(updateId).update({ data: updateData })
			ctx.data.getResult = getData
			ctx.body = { code: 200, msg: "成功", data: ctx.data.getResult }
		} catch (error) {
			ctx.body = { code: 301, msg: error.message, data: {} }
		}
	})

	app.router('getFirst', async (ctx, next) => {
		try {
			const getData = await db.collection('pet').where(queryData).get()
			if (getData.data.length > 0) {
				ctx.data.getResult = getData
				ctx.body = { code: 200, msg: "成功", data: getData.data[0] }
			} else {
				ctx.body = { code: 301, msg: "失败", data: {} }
			}
		} catch (error) {
			ctx.body = { code: 301, msg: error.message, data: {} }
		}
	})
	app.router('query', async (ctx, next) => {
		try {
			const getData = await db.collection('pet').where(queryData).get()
			if (getData.data.length > 0) {
				ctx.data.getResult = getData
				ctx.body = { code: 200, msg: "成功", data: getData.data }
			} else {
				ctx.body = { code: 301, msg: "失败", data: {} }
			}
		} catch (error) {
			ctx.body = { code: 301, msg: error.message, data: {} }
		}
	})

	app.router('getMyPet', async (ctx, next) => {
		try {
			const getData = await db.collection('pet').where({ open_id: OPENID }).get()
			if (getData.data.length > 0) {
				ctx.data.getResult = getData
				ctx.body = { code: 200, msg: "成功", data: getData.data }
			} else {
				ctx.body = { code: 301, msg: "失败", data: {} }
			}
		} catch (error) {
			ctx.body = { code: 301, msg: error.message, data: {} }
		}
	})
	
	app.router('delMyPet', async(ctx, next) => {
		try {
			await db.collection('pet').doc(petId).remove()
			ctx.body = { code: 200, msg: "成功", data: {} }
		}catch(error) {
			ctx.body = { code: 301, msg: error.message, data: {} }
		}
	})

	app.router('getDisease', async (ctx, next) => {
		try {
			const getData = await db.collection('disease').where(queryData).get()
			if (getData.data.length > 0) {
				ctx.data.getResult = getData
				ctx.body = { code: 200, msg: "成功", data: getData.data }
			} else {
				ctx.body = { code: 301, msg: "失败", data: {} }
			}
		} catch (error) {
			ctx.body = { code: 301, msg: error.message, data: {} }

		}

	})
	app.router('getFirstDisease', async (ctx, next) => {
		try {
			const getData = await db.collection('disease').where(queryData).get()
			if (getData.data.length > 0) {
				ctx.data.getResult = getData
				ctx.body = { code: 200, msg: "成功", data: getData.data[0] }
			} else {
				ctx.body = { code: 301, msg: "失败", data: {} }
			}

		} catch (error) {
			ctx.body = { code: 301, msg: error.message, data: {} }

		}
	})

	app.router('saveIdentify', async (ctx, next) => {
		try {
			const addRes = await db.collection('pet_identify').add({
				data: {
					...addPetIdentify,
					open_id: OPENID,
					create_time: new Date()
				}
			})
			ctx.body = { code: 200, msg: "成功", data: addRes }
		} catch (error) {
			ctx.body = { code: 301, msg: error.message, data: {} }
		}
	})
	app.router('getIdentify', async (ctx, next) => {
		try {
			const getData = await db.collection('pet_identify').where({open_id: OPENID}).orderBy('create_time', 'desc').get()
			if (getData.data.length > 0) {
				ctx.data.getResult = getData
				ctx.body = { code: 200, msg: "成功", data: getData.data }
			} else {
				ctx.body = { code: 301, msg: "失败", data: {} }
			}
		} catch (error) {
			ctx.body = { code: 301, msg: error.message, data: {} }

		}
	})
	app.router('getIdentifyById', async(ctx, next) => {
		try {
			const getData = await db.collection('pet_identify').doc(identifyId).get()
			ctx.body = {code: 200, msg: "成功", data: getData.data}
		}catch (error) {
			ctx.body = { code: 301, msg: error.message, data: {} }
		}
	})
	app.router('delIdentify', async(ctx, next) => {
		try {
			const delRes = await db.collection('pet_identify').where({_id: identifyId}).remove()
			ctx.body = { code: 200, msg: "成功", data: {} }
		}catch(error) {
			ctx.body = { code: 301, msg: error.message, data: {} }
		}
	})
	return app.serve()
}